syntax/sh: Improve heredoc detection#3931
Conversation
- allow spaces between optional '-' and delimiter - allow additional characters as delimiter
|
@Andriamanitra: |
| start: "<<-?[\\s]*[\\w,.:~#!§$%=?@*+-]+$" | ||
| end: "^[\\w,.:~#!§$%=?@*+-]+$" |
There was a problem hiding this comment.
Do people actually use punctuation for defining heredocs? I think it would be beneficial to keep the set of characters more realistic to reduce likelihood of false positives when looking for the end token.
There was a problem hiding this comment.
I don't really know if they are used, but they are at least allowed. I tried them with the bash.
In case we ignore a valid character there will most probably be a ticket complaining about it. 🤷♂️
Edit:
By taking a closer look at [\\s] the [] can be removed.
There was a problem hiding this comment.
I've never seen punctuation used for heredocs but I guess some other editors also allow it (although they don't need to worry about false positives for the end delimiter):
- VS Code: allows
[^\"' \\t]+ - Sublime Text: allows
[[:alpha:]_][[:alnum:]_.-]* - neovim: allows
[^ \t|>]+
|
We should probably handle quotes. GNU Bash reference manual says:
I've gathered some examples of heredocs below. This PR has some issues when heredoc is in the middle of a command, and when there are # common usage
cat << EOF
hello ' " world
EOF
cat << 'EOF'
hello ' " world
EOF
cat <<-"EOF"
hello ' " world
EOF
# from https://github.com/openwrt/openwrt
cat <<-EOT | "${CC:-false}" $CFLAGS -o /dev/null -x c - 2>/dev/null
#include <stdio.h>
int main(int argc, char **argv) {
printf("Hello, world!\n");
return 0;
}
EOT
# from https://github.com/goToMain/libosdp/blob/e586d3f700215d16b0e79513880d1c1ade815ba0/configure.sh#L9-L32
cat >&2<<----
LibOSDP build configure script
OPTIONS:
--packet-trace Enable raw packet trace for diagnostics
--data-trace Enable command/reply data buffer tracing
--skip-mark Don't send the leading mark byte (0xFF)
--crypto LIB Use methods from LIB (openssl/mbedtls/*tinyaes)
---
# from https://github.com/mingw-w64/mingw-w64/blob/c2167bc6da600f7fdbd131734767a67ffb9e970e/mingw-w64-headers/configure#L1281
cat <<_ACEOF
'configure' configures mingw-w64-headers 4.0b to adapt to many kinds of systems.
Usage: $0 [OPTION]... [VAR=VALUE]...
Configuration:
-h, --help display this help and exit
--help=short display options specific to this package
--help=recursive display the short help of all the included packages
-V, --version display version information and exit
-q, --quiet, --silent do not print 'checking ...' messages
_ACEOF
# from https://github.com/mingw-w64/mingw-w64/blob/c2167bc6da600f7fdbd131734767a67ffb9e970e/mingw-w64-tools/genpeimg/build-aux/compile#L240
cat <<\EOF
Usage: compile [--help] [--version] PROGRAM [ARGS]
Wrapper for compilers which do not understand '-c -o'.
Remove '-o dest.o' from ARGS, run PROGRAM with the remaining
arguments, and rename the output as expected.
EOF |
|
You're right. |
Fixes #3927